﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Flow;

namespace Think9.Controllers.Basic
{
    [Area("SysFlow")]
    public class PrcsNextController : BaseController
    {
        private FlowPrcsNextService FlowPrcsNextService = new FlowPrcsNextService();
        private SmsService SmsService = new SmsService();
        private ComService ComService = new ComService();

        [HttpGet]
        public ActionResult Next(string listid, string flid)
        {
            string msm = "";
            string sql = "select * FROM flowrunlist where listid=" + listid + " ";
            DataTable dt = ComService.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                msm = "编号为--" + dt.Rows[0]["RuNumber"].ToString() + "名称为--" + dt.Rows[0]["RunName"].ToString() + "已交由你办理";
            }
            ViewBag.msm = msm;
            ViewBag.ListId = listid;
            ViewBag.flid = flid;

            CurrentPrcsEntity mPrcs = FlowCom.GetCurrentFlowStept(flid, listid);

            if (mPrcs.FlowType == "1")
            {
                return View("~/Areas/SysFlow/PrcsNext/PrcsNext1.cshtml");//固定
            }
            else
            {
                return View("~/Areas/SysFlow/PrcsNext/PrcsNext2.cshtml");//自由
            }
        }

        //固定流程返回下一步骤
        [HttpPost]
        public ActionResult GetNextStepList(string listid, string flid)
        {
            listid = listid == null ? "0" : listid;
            var result = new { code = 0, msg = "", count = 999999, data = FlowPrcsNextService.GetNextPrcsList(listid, flid) };
            return Json(result);
        }

        //固定流程返回回退步骤
        [HttpPost]
        public ActionResult GetBackStepList(string listid, string flid)
        {
            listid = listid == null ? "0" : listid;
            var result = new { code = 0, msg = "", count = 999999, data = FlowPrcsNextService.GetBackPrcsList(listid, flid) };
            return Json(result);
        }

        //固定流程 转交
        [HttpPost]
        public ActionResult FlowPrcsNext01(string listid, IEnumerable<valueTextEntity> list)
        {
            string err = "";
            string selectUser = BasicHelp.GetTextFromList(list, "classid", "selectUserId").Trim();
            string nextId = BasicHelp.GetTextFromList(list, "classid", "selectPrcsId").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks").Trim();

            string isSelectUser = BasicHelp.GetTextFromList(list, "classid", "ckSelectUser").Trim();
            if (isSelectUser == "true")
            {
                selectUser = "";
            }

            string ispublic = "2";
            if (BasicHelp.GetTextFromList(list, "classid", "isPublic").Trim() == "true")
            {
                ispublic = "1";
            }

            try
            {
                err = FlowCom.SetFlowPrcsNext01(CurrentUser, listid, nextId, selectUser, remarks, ispublic);

                if (err == "")
                {
                    string account = CurrentUser == null ? "un_defined" : CurrentUser.Account;
                    SmsService.AddSms(account, selectUser, msg, 99);

                    return Json(SuccessTip("转交成功"));
                }
                else
                {
                    return Json(ErrorTip(err));
                }
            }
            catch (Exception ex)
            {
                return Json(ErrorTip(ex.Message));
            }
        }

        //固定流程
        [HttpPost]
        public ActionResult FlowPrcsBack01(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserId").Trim();
            string selectid = BasicHelp.GetTextFromList(list, "classid", "selectPrcsId").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks").Trim();
            string ispublic = "1";

            try
            {
                string err = FlowCom.SetFlowPrcsBack01(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

                if (err == "")
                {
                    return Json(SuccessTip("转交成功"));
                }
                else
                {
                    return Json(ErrorTip(err));
                }
            }
            catch (Exception ex)
            {
                return Json(ErrorTip(ex.Message));
            }
        }

        //自由流程
        [HttpPost]
        public ActionResult FlowPrcsNext02(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserV").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg01").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks01").Trim();
            string selectid = "";
            string ispublic = "1";

            if (string.IsNullOrEmpty(selectUserV))
            {
                return Json(ErrorTip("请选择办理人！"));
            }

            try
            {
                string err = FlowCom.SetFlowPrcsNext02(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

                if (err == "")
                {
                    return Json(SuccessTip("转交成功"));
                }
                else
                {
                    return Json(ErrorTip(err));
                }
            }
            catch (Exception ex)
            {
                return Json(ErrorTip(ex.Message));
            }
        }

        //自由流程
        [HttpPost]
        public ActionResult FlowPrcsFinish(string listid, IEnumerable<valueTextEntity> list)
        {
            string selectUserV = BasicHelp.GetTextFromList(list, "classid", "selectUserV").Trim();
            string msg = BasicHelp.GetTextFromList(list, "classid", "msg01").Trim();
            string remarks = BasicHelp.GetTextFromList(list, "classid", "remarks01").Trim();
            string selectid = "0";
            string ispublic = "1";

            try
            {
                string err = FlowCom.SetFlowPrcsNext02(CurrentUser, listid, selectid, selectUserV, remarks, ispublic);

                if (err == "")
                {
                    return Json(SuccessTip("转交成功"));
                }
                else
                {
                    return Json(ErrorTip(err));
                }
            }
            catch (Exception ex)
            {
                return Json(ErrorTip(ex.Message));
            }
        }
    }
}